package com.kgc.provider;

import com.alibaba.dubbo.config.annotation.Service;

import com.alibaba.fastjson.JSON;
import com.kgc.api.goods.ShoppingCartApi;
import com.kgc.bean.GoodsItem;
import com.kgc.bean.ShoppingCart;
import com.kgc.bean.ShoppingCartExample;
import com.kgc.mapper.GoodsItemMapper;
import com.kgc.mapper.OrderMapper;
import com.kgc.mapper.ShoppingCartMapper;
import com.kgc.util.RedisUtils;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartProvider implements ShoppingCartApi {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private OrderMapper orderMapper;


    @Override
    public Boolean userIsHaveShoppingCart(Integer userId) {
        ShoppingCartExample cartExample=new ShoppingCartExample();
        ShoppingCartExample.Criteria criteria=cartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        if(shoppingCartMapper.selectByExample(cartExample).size()>0){
            return true;
        }
        return false;
    }

    @Override
    public ShoppingCart queryUserShoppingCart(Integer userId,String goodsId) {
        ShoppingCartExample cartExample=new ShoppingCartExample();
        ShoppingCartExample.Criteria criteria=cartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
//        log.info("-----");
        return shoppingCartMapper.selectByExample(cartExample).get(0);
    }

    @Override
    public Boolean assignAShoppingCartToTheUser(Integer userId, String goodsId,Integer businessid) {
//        List<GoodsItem> shopcarlist =  orderMapper.queryorderitem(goodsId);
//        log.info("存入数据为:{}",shopcarlist.toString());
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(userId);
//        shoppingCart.setCarinfolist(JSON.toJSONString(redisUtils.get("cart:"+userId)));
//        log.info("------数据:{}",redisUtils.get("cart:"+userId).toString());
//        shoppingCart.setCarinfolist(JSON.toJSONString(shopcarlist));
        if(shoppingCartMapper.insert(shoppingCart)==1){
            return true;
        }
        return false;
    }

    @Override
    public List<ShoppingCart> getAllUsersWhoHaveAShoppingCart() {
        return shoppingCartMapper.selectAll();
    }

    @Override
    public Boolean persistentShoppingCart(ShoppingCart shoppingCart) {
        if(shoppingCartMapper.updateByPrimaryKey(shoppingCart)==1){
            return true;
        }
        return false;
    }
}
